using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace OnlineAnadolu
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bt_Kaydet_Click(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "Select user_name from users where user_name=N'" + tb_KullaniciAdi.Text + "'";
           // GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
            if (GridView1.Rows.Count == 0)
            {
                Session["stateid"] = ddl_Semt.SelectedValue;
                Session["localid"] = ddl_Mahalle.SelectedValue;
                Session["departmentid"] = ddl_Bolum.SelectedValue;
                tb_LastVisit.Text = DateTime.Now.ToString();

                SqlDataSource1.InsertCommand = "INSERT INTO address(addressline1, addressline2, stateid, localid)VALUES(N'" + tb_Adres.Text + "',N'" + tb_Adres2.Text + "'," + ddl_Semt.SelectedValue + "," + ddl_Mahalle.SelectedValue + ")";
                SqlDataSource1.Insert();

                string c = tb_EMail.Text + ddl_EMAil.SelectedItem.Text;
                SqlDataSource1.InsertCommand = "INSERT INTO contact(phone, phone2, email)VALUES(N'" + tb_EvTelefon.Text + "',N'" + TextBox7.Text + "',N'" + c + "')";
                SqlDataSource1.Insert();

                SqlDataSource1.SelectCommand = "SELECT  MAX(addressid) AS Expr1 FROM address";
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
                string adr = GridView1.Rows[0].Cells[0].Text;


                SqlDataSource1.SelectCommand = "SELECT  MAX(contactinfoid) AS Expr1 FROM contact";
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
                string phone = GridView1.Rows[0].Cells[0].Text;


                Random rn = new Random();
                int a = rn.Next(1000, 65535);


                string body = "Sayin " + tb_Name.Text + " Kullanici Adiniz:"+tb_KullaniciAdi.Text + " Onay Kodunuz:" + a + "<br>" + "<url>" + "http://hakanerdogan.net" + "</url>" + "</br>";
                Mail mago = new Mail();
                mago.MailGonder(c, body);

                SqlDataSource1.InsertCommand = "INSERT INTO users(user_name,endusertypeid, firstname, contactid,addressid,gender,issubscribed,lastvisit,subscribecode,departmentid)VALUES(N'" + tb_KullaniciAdi.Text + "',2,N'" + tb_Name.Text + "','" + phone + "','" + adr + "',N'" + ddl_Cinsiyet.SelectedItem.Text + "',0,'" + DateTime.Now.ToString() + "',N'" + a.ToString() + "','" + ddl_Bolum.SelectedValue + "')";
                SqlDataSource1.Insert();
                Label1.Text = "Mailinizi kontrol ediniz";

            }
            else
                Label1.Text = "Boyle bir kullanici mevcut.";
        }

        protected void tb_EMail_TextChanged(object sender, EventArgs e)
        {
            Label2.Text = "";
            
            string a = tb_EMail.Text + ddl_EMAil.SelectedItem.Text;
            SqlDataSource6.SelectCommand = "SELECT contact.email FROM contact INNER JOIN users ON contact.contactinfoid = users.contactid WHERE (users.issubscribed = 1) AND (contact.email = N'"+a+"')";
            GridView2.DataBind();
            if (GridView2.Rows.Count != 0)
            {
                Label2.Text = "Bu email kayitli";
                tb_EMail.Text = "";
            }

        }

        protected void tb_KullaniciAdi_TextChanged(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "Select user_name from users where user_name=N'" + tb_KullaniciAdi.Text + "'";
            GridView1.DataBind();
            if (GridView1.Rows.Count != 0)
            {
                lb_KullaniciAdiKontrol.Text = "Boyle bir kullanici mevcut";
                tb_KullaniciAdi.Text = "";
            }
            else
                lb_KullaniciAdiKontrol.Text = "Kullanici Adi uygun";
        }
    }
}
